package cloud.maque.auth.service.impl;


import cloud.maque.auth.entity.AuthUser;
import cloud.maque.auth.entity.AuthUserThrid;
import cloud.maque.auth.mapper.AuthUserThridMapper;
import cloud.maque.auth.service.UserService;
import cloud.maque.auth.service.UserThridService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserThridServiceImpl extends ServiceImpl<AuthUserThridMapper, AuthUserThrid> implements UserThridService {

    @Autowired
    UserService userService;

    /**
     * 根据第三方类型和ID 查询用户
     *
     * @param thridType
     * @param thridUserId
     * @return
     */
    @Override
    public AuthUserThrid findThridByUser(String thridType, String thridUserId) {

        QueryWrapper<AuthUserThrid> queryWrapper = new QueryWrapper<>();
        //类型全部大写
        queryWrapper.lambda().eq(AuthUserThrid::getThridType, thridType)
                .eq(AuthUserThrid::getThridUserId, thridUserId);

        return this.getOne(queryWrapper);
    }

    /**
     * 根据第三方类型和ID 查询用户
     *
     * @param thridType
     * @param thridUserId
     * @return
     */
    @Override
    public AuthUser findByUser(String thridType, String thridUserId) {

        AuthUserThrid authUserThrid = this.findThridByUser(thridType, thridUserId);
        if (authUserThrid != null) {
            return userService.getUserByUserId(authUserThrid.getUserId());
        }
        return null;
    }

    /**
     * 创建第三方数据
     *
     * @param authUserThrid
     * @return
     */
    @Override
    public AuthUserThrid SaveThrid(AuthUserThrid authUserThrid) {
        AuthUserThrid thrid = this.findThridByUser(authUserThrid.getThridType(), authUserThrid.getThridUserId());
        if (thrid == null) {
            saveOrUpdate(authUserThrid);
            return authUserThrid;
        } else {
            return thrid;
        }
    }
}
